package com.example.android.sensortest;

import android.app.Activity;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

public class SensorsActivity extends Activity {
    SensorManager mSensorManager;

    List<Sensor> mSensors;
    List<SensorEventListener> mSensorEventListeners = new ArrayList<SensorEventListener>();

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tab_sensors);

        final LinearLayout container = (LinearLayout) findViewById(R.id.container);

        // Get a handle to the SensorManager
/*
        mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
*/

        // Get a list of all available sensors. Generally you'd want to just use
        // getDefaultSensor(type)
/*
        mSensors = mSensorManager.getSensorList(Sensor.TYPE_ALL);
*/

        // We'll use this to load a layout template for each sensor
        final LayoutInflater layoutInflater = getLayoutInflater();

        for (Sensor sensor : mSensors) {

            // Inflate the layout template and get handles to the individual views it contains
            final View sensorRootView = layoutInflater
                    .inflate(R.layout.item_sensor, container, false);
            final TextView titleView = (TextView) sensorRootView.findViewById(R.id.sensor_title);
            final TextView metadataView = (TextView) sensorRootView.findViewById(
                    R.id.sensor_metadata);
            final TextView valuesView = (TextView) sensorRootView.findViewById(R.id.sensor_values);
            final TextView accuracyView = (TextView) sensorRootView.findViewById(R.id.sensor_accuracy);

            // Set the title and metadata
            titleView.setText(sensor.getName());

            final StringBuilder sb = new StringBuilder();
/*
            sb.append("Type: " + getSensorTypeString(sensor.getType()));
            sb.append("\nVendor: " + sensor.getVendor());
            sb.append("\nVersion: " + sensor.getVersion());
            sb.append("\nResolution: " + sensor.getResolution());
            sb.append("\nPower: " + sensor.getPower());
            sb.append("\nMin delay: " + sensor.getMinDelay());
            sb.append("\nMax range: " + sensor.getMaximumRange());
            metadataView.setText(sb.toString());
*/

            // Create a new SensorEventListener that will update the values and accuracy views
            // for the sensor when the sensor reports data changes.
            mSensorEventListeners.add(new SensorEventListener() {
                public void onSensorChanged(SensorEvent sensorEvent) {
                    // Reset the StringBuilder
                    sb.setLength(0);

                    // Sensors can report multiple values; we'll just print them all out here
/*
                    sb.append("Values: ");
                    for (int i = 0; i < sensorEvent.values.length; i++) {
                        sb.append("\n" + sensorEvent.values[i]);
                    }
*/
                    valuesView.setText(sb.toString());

                    accuracyView.setText("Accuracy: " + sensorEvent.accuracy);
                }

                public void onAccuracyChanged(Sensor sensor, int accuracy) {
                    accuracyView.setText("Accuracy: " + accuracy);
                }
            });

            container.addView(sensorRootView);
        }
    }

    @Override
    protected void onResume() {
        super.onResume();

        // Resume is called after onCreate when the activity is first loaded, and after it's
        // put back into the foreground. In our onResume, we'll register our listeners, that
        // we created earlier on in onCreate.

        if (mSensors == null || mSensorManager == null) {
            return;
        }

        for (int i = mSensors.size() - 1; i >= 0; i--) {
/*
            mSensorManager.registerListener(
                    mSensorEventListeners.get(i),           // The event listener
                    mSensors.get(i),                        // The sensor
                    SensorManager.SENSOR_DELAY_NORMAL);     // The speed with which we want updates
*/
        }
    }

    @Override
    protected void onPause() {
        super.onPause();

        // onPause is called when the activity is backgrounded. We'll want to unregister our
        // listeners since we don't care to receive updates while we're not in the foreground.

        if (mSensors == null || mSensorManager == null) {
            return;
        }

        for (int i = mSensors.size() - 1; i >= 0; i--) {
/*
            mSensorManager.unregisterListener(mSensorEventListeners.get(i));
*/
        }
    }

    private static String getSensorTypeString(int type) {
        switch (type) {
            case Sensor.TYPE_ACCELEROMETER:
                return "TYPE_ACCELEROMETER";
            case Sensor.TYPE_GRAVITY:
                return "TYPE_GRAVITY";
            case Sensor.TYPE_GYROSCOPE:
                return "TYPE_GYROSCOPE";
            case Sensor.TYPE_LIGHT:
                return "TYPE_LIGHT";
            case Sensor.TYPE_LINEAR_ACCELERATION:
                return "TYPE_LINEAR_ACCELERATION";
            case Sensor.TYPE_MAGNETIC_FIELD:
                return "TYPE_MAGNETIC_FIELD";
            case Sensor.TYPE_ORIENTATION:
                return "TYPE_ORIENTATION (deprecated.. use SensorManager.getOrientation)";
            case Sensor.TYPE_PRESSURE:
                return "TYPE_PRESSURE";
            case Sensor.TYPE_PROXIMITY:
                return "TYPE_PROXIMITY";
            case Sensor.TYPE_ROTATION_VECTOR:
                return "TYPE_ROTATION_VECTOR";
            case Sensor.TYPE_TEMPERATURE:
                return "TYPE_TEMPERATURE";
        }
        return "(Unknown type)";
    }
}
